package com.zxy.ziems.server.statistics.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zxy.ziems.server.mybatis.core.service.ServiceImplX;
import com.zxy.ziems.server.statistics.CacheDataMapper;
import com.zxy.ziems.server.statistics.pojo.entity.CacheData;
import com.zxy.ziems.server.statistics.service.CacheDataService;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CacheDataServiceImpl extends ServiceImplX<CacheDataMapper, CacheData> implements CacheDataService {
    @Override
    public CacheData getByTypeAndIndex(String type, String index) {
        QueryWrapper<CacheData> param = new QueryWrapper<>();
        param.lambda().eq(CacheData::getType, type).eq(CacheData::getIndex, index);

        List<CacheData> cacheData = baseMapper.selectList(param);
        return cacheData.isEmpty() ? null : cacheData.get(0);
    }

    @Override
    public void save(String type, String index, Object o) {
        CacheData entity = new CacheData();
        entity.setType(type);
        entity.setIndex(index);
        entity.setContent(JSONObject.toJSONString(o));

        baseMapper.insert(entity);
    }


}
